<template>
  <div class="AdendDialog">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="24">
          <el-form-item label="字典名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="描述">
            <el-input v-model="ruleForm.description" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="排序">
            <el-input v-model="ruleForm.sort" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: ["row", "dialog"],
    data() {
      return {
        ruleForm: {
          name: "",
          description: "",
          sort: "",
          pCode: ""
        },
        rules: {
          name: [{
            required: true,
            message: "请输入字典名称",
            trigger: "blur"
          }]
        }
      };
    },
    watch: {
      dialog: {
        handler() {
          if (this.dialog) {
            if (this.$refs["ruleForm"]) {
              this.$refs["ruleForm"].resetFields();
            }
            Object.keys(this.ruleForm).forEach(key => {
              this.ruleForm[key] = "";
            });
            this.ruleForm.pCode = this.row.code;
          }
        },
        immediate: true, // immediate选项可以开启首次赋值监听
        deep: true
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.$server.addDict(this.ruleForm).then(res => {
              const val = {
                flag: true
              };
              this.$emit("chang", val);
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      resetForm(formName) {
        const val = {
          flag: false
        };
        this.$emit("chang", val);
      }
    }
  };
</script>
<style lang="scss" scoped>
  .AdendDialog {}
</style>
